class JcEvent{
    // 单例    
    public static get ins() : JcEvent{
        if(JcEvent._ins == null){
            JcEvent._ins = new JcEvent();
        }
        return JcEvent._ins;
    }

    // 注册事件
    public RegistEvent(name : string, callBack : Function, arg : any){
        let obsvr : Observer[] = this._events[name];
        if(!obsvr){
            this._events[name] = [];
        }
        this._events[name].push(new Observer(callBack, arg));
    }

    // 移除事件
    public RemoveEvent(name : string, callBack : Function, arg : any){
        let obsvrs : Observer[] = this._events[name];
        if(!obsvrs) return;
        let len = obsvrs.length;
        for(let i = 0; i < len; i++){
            let obsvr = obsvrs[i];
            if(obsvr.compar(arg)){
                obsvrs.splice(i, 1);
                break;
            }
        }
        if(obsvrs.length == 0){
            delete this._events[name];
        }
    }

    // 通知事件
    public NotifyEvent(name : string, ...args:any[]){
        let obsvrs : Observer[] = this._events[name];
        if(!obsvrs) return;
        let len = obsvrs.length;

        for(let i = 0; i < len; i++){
            let obsver = obsvrs[i];
            obsver.Notify(name, ...args);
        }
    }


    private _events = {};
    private static _ins : JcEvent;

    private constructor(){

    }
}


class Observer{
    private _arg : any = null;
    private _callBack : Function = null;

    public constructor(callBack : Function, arg : any ){
        this._arg = arg;
        this._callBack = callBack;
    }

    public Notify(...args : any[]){
        this._callBack.call(this._arg, ...args);
    }

    public compar(arg : any) : boolean{
        return this._arg == arg;
    }
}